package com.zzj.ecology.admin.advice;

import com.zzj.ecology.commons.exception.TgfException;
import com.zzj.ecology.system.dto.R;
import org.apache.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName ExceptionAdvisor
 * @Description TODO
 * @Author zhijun zeng at 2020/2/29
 * @Version 1.0
 **/
@RestControllerAdvice
//@Slf4j
public class CustomizeUnifiedExceptionsAdvice {

//    @ExceptionHandler(Exception.class)
//    public HttpResult userLoginExcetion(Exception e) {
//        if (e instanceof LoginException) {
//            //String code = e.getMessage();
//            //String nameByCode = LoginResultEnum.getNameByCode(code);
//            //return new HttpResult(code, nameByCode);
//        } else if (e instanceof IOException) {
//            return HttpResult.error().add(HttpResult.DATA_TAG, e);
//        }
//        return HttpResult.error().add(HttpResult.DATA_TAG, e);
//    }

    @ExceptionHandler(TgfException.class)
    public R tgfException(TgfException e) {
        // unauthorized
        if (e.getCode() == HttpStatus.SC_UNAUTHORIZED) {
            return new R(e.getCode(), e.getMsg());
        }

        return R.ng();
    }
}
